package com.deyuanyun.pic.wf.service.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.deyuanyun.pic.settings.domain.util.DomainInitUtil;
import com.deyuanyun.pic.wf.domain.ProcessDefinition;
import com.deyuanyun.pic.wf.domain.ProcessDefinitionExample;
import com.deyuanyun.pic.wf.domain.ProcessInstance;
import com.deyuanyun.pic.wf.mapper.ProcessDefinitionMapper;
import com.deyuanyun.pic.wf.mapper.ProcessInstanceMapper;
import com.deyuanyun.pic.wf.mapper.ProcessMapper;
import com.deyuanyun.pic.wf.service.ProcessDefinitionService;
import com.deyuanyun.pic.wf.service.RepairWorkflowService;

@Service
public class ProcessDefinitionServiceImpl implements ProcessDefinitionService {
    @Autowired
    private ProcessDefinitionMapper processDefinitionMapper;
    @Autowired
    private ProcessInstanceMapper processInstanceMapper;
    @Autowired
    private RepairWorkflowService repairWorkflowService;
    @Autowired
    private ProcessMapper processMapper;

    private static final Logger logger = LoggerFactory.getLogger(ProcessDefinitionServiceImpl.class);

    public ProcessDefinition selectByPrimaryKey(String id) {
        return this.processDefinitionMapper.selectByPrimaryKey(id);
    }

    public int deleteByPrimaryKey(String id) {
        return this.processDefinitionMapper.deleteByPrimaryKey(id);
    }

    public int updateByPrimaryKeySelective(ProcessDefinition record) {
        return this.processDefinitionMapper.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(ProcessDefinition record) {
        return this.processDefinitionMapper.updateByPrimaryKey(record);
    }

    public int insert(ProcessDefinition record) {
        return this.processDefinitionMapper.insert(record);
    }

    public List<ProcessDefinition> queryProcessDefinitionPbf(){
        ProcessDefinitionExample example = new ProcessDefinitionExample();

        example.createCriteria().andDeletedEqualTo((short)0).andCodeNotEqualTo("OTHER_REPAIR_CORL");
        example.setOrderByClause("CREATE_DATE");
        List<ProcessDefinition> pd_list = processDefinitionMapper.selectByExample(example);

        return pd_list;
    }

    public List<ProcessDefinition> queryProcessDefinitionPbfAll(){
        ProcessDefinitionExample example = new ProcessDefinitionExample();

        example.createCriteria().andCodeNotEqualTo("OTHER_REPAIR_CORL");
        example.setOrderByClause("CREATE_DATE");
        List<ProcessDefinition> pd_list = processDefinitionMapper.selectByExample(example);

        return pd_list;
    }


    public List<ProcessDefinition> queryProcessDefinitionCoat(){
        ProcessDefinitionExample example = new ProcessDefinitionExample();

        example.createCriteria().andDeletedEqualTo((short)0).andCodeEqualTo("OTHER_REPAIR_CORL");
        example.setOrderByClause("CREATE_DATE");
        List<ProcessDefinition> pd_list = processDefinitionMapper.selectByExample(example);

        return pd_list;
    }
    
    @Override
    public List<Map<String, Object>> queryProcessName() {

        List method=processMapper.queryProcessName();

        return method;
    }

    //update pic_wf_process_instance set pic_wf_process_instance.deleted = 1 where DEFINITION_ID in (select id_ from pic_wf_process_definition where deleted = 1)
    public void updateToDelete(String id){
        ProcessDefinition processDefinition = new ProcessDefinition();
        processDefinition.setId(id);
        processDefinition.setDeleted((short)1);
        DomainInitUtil.initUpdateDomain(processDefinition,"标记为删除");
        
        processDefinitionMapper.updateByPrimaryKeySelective(processDefinition);
        
        List<ProcessInstance> pi_list = repairWorkflowService.queryProcessInstance(id);
        for (int i = 0; i < pi_list.size(); i++) {
            ProcessInstance pi = pi_list.get(i);
            pi.setDeleted((short)1);
            DomainInitUtil.initUpdateDomain(pi,"标记为删除");
            processInstanceMapper.updateByPrimaryKeySelective(pi);
        }
    }
    
    
}